package ${package.Controller};

import com.github.pagehelper.Page;
import com.sieiot.goodknow.base.stable.api.PageQO;
import com.sieiot.goodknow.base.stable.api.TPageResult;
import com.sieiot.goodknow.base.stable.api.TResult;
import com.sieiot.goodknow.common.util.PageUtil;
<#if swagger2>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
</#if>
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>

import javax.annotation.Resource;
import javax.validation.Valid;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if swagger2>
@Api(tags = {"${entity}"})
</#if>
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
<#assign rpath = "">
<#if package.ModuleName?? && package.ModuleName != "">
    <#assign rpath = "${package.ModuleName}">
</#if>
<#if controllerMappingHyphenStyle??>
    <#assign rpath = "${rpath}/${controllerMappingHyphen}">
<#else>
    <#assign rpath = "${rpath}/${table.entityPath}">
</#if>
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    /** 新增 */
    final String INSERT = "${rpath}/insert";
    /** 依据主键更新 */
    final String UPDATE_BY_ID = "${rpath}/update-by-id";
    /** 依据主键删除 */
    final String DELETE_BY_ID = "${rpath}/delete-by-id";
    /** 依据主键查询一条数据 */
    final String QUERY_BY_ID = "${rpath}/query-by-id";
    /** 依据多主键查询数据 */
    final String QUERY_BY_IDS = "${rpath}/query-by-ids";
    /** 多条件查询 */
    final String PAGE_QUERY = "${rpath}/page-query";

    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};

    <#if swagger2>
    @ApiOperation(value = "新增", notes = "新增 ${entity} 记录")
    <#else>
    /**
     * 新增 ${entity} 记录
     * @param qo 数据对象
     * @return TResult<Boolean>
     */
    </#if>
    @PostMapping(value = INSERT)
    public TResult<Boolean> insert(@Valid @RequestBody ${entity} qo) {

        return TResult.resp(${table.serviceName?uncap_first}.insert(qo));
    }

    <#if swagger2>
    @ApiOperation(value = "更新", notes = "更新 ${entity} 记录")
    <#else>
    /**
     * 更新 ${entity} 记录
     * @param qo 数据对象
     * @return TResult<Boolean>
     */
    </#if>
    @PostMapping(value = UPDATE_BY_ID)
    public TResult<Boolean> update(@Valid @RequestBody ${entity} qo) {

        return TResult.resp(${table.serviceName?uncap_first}.update(qo));
    }

    <#if swagger2>
    @ApiOperation(value = "删除", notes = "删除指定的 ${entity} 记录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "ID主键", required = true, paramType = "query", dataType = "Long")
    })
    <#else>
    /**
     * 删除指定的 ${entity} 记录
     * @param id 主键
     * @return TResult<Boolean>
     */
    </#if>
    @PostMapping(value = DELETE_BY_ID)
    public TResult<Boolean> deleteById(Long id) {

        return TResult.resp(${table.serviceName?uncap_first}.deleteById(id));
    }

    <#if swagger2>
    @ApiOperation(value = "ID主键查找", notes = "ID主键查找 ${entity} 对象数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "ID主键", required = true, paramType = "query", dataType = "Long")
    })
    <#else>
    /**
     * ID主键查找 ${entity} 对象数据
     * @param id 主键
     * @return TResult<${entity}>
     */
    </#if>
    @PostMapping(value = QUERY_BY_ID)
    public TResult<${entity}> queryById(Long id) {
        ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.queryById(id);
        return TResult.success(${entity?uncap_first});
    }

    <#if swagger2>
    @ApiOperation(value = "多ID主键查找", notes = "多ID主键查找 ${entity} 对象数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "idList", value = "ID主键列表", required = true, paramType = "query", dataType = "List")
    })
    <#else>
    /**
     * 多ID主键查找 ${entity} 对象数据
     * @param idList 多主键
     * @return TResult<List<${entity}>>
     */
    </#if>
    @PostMapping(value = QUERY_BY_IDS)
    public TResult<List<${entity}>> queryByIds(List<Long> idList) {
        List<${entity}> list = ${table.serviceName?uncap_first}.queryByIds(idList);
        return TResult.success(list);
    }

    <#if swagger2>
    @ApiOperation(value = "分页查询", notes = "分页查询数据列表")
    <#else>
    /**
     * 分页查询
     * @param pageQO 分页查询对象
     * @return TResult<TPageResult<${entity}>>
     */
    </#if>
    @PostMapping(value = PAGE_QUERY)
    public TResult<TPageResult<${entity}>> pageQuery(@RequestBody PageQO<${entity}> pageQO) {
        Page<${entity}> page = ${table.serviceName?uncap_first}.pageQuery(pageQO);
        TPageResult<${entity}> pageResult = PageUtil.restPage(page);
        return TResult.success(pageResult);
    }

}
</#if>
